<template>
    <div>
        <div>
        <van-nav-bar title="修改密码" left-arrow  @click-left="onClickLeft"/> 
        </div>
        <div align="center" style="position:relative; top:30px" v-if="show">
          <div>
              <van-cell-group>
                <van-field
                    id="userName"
                    v-model="userName"
                    label="用户名"
                    placeholder="请输入用户名"
                    required
                />

                <van-field
                    id="stuID"
                    v-model="stuID"
                    label="学号"
                    placeholder="请输入学号"
                    required
                />

                <van-field
                    id="phone"
                    v-model="phone"
                    label="手机"
                    placeholder="请输入手机号码"
                    required
                />

                <van-field
                    id="IDcard"
                    v-model="IDcard"
                    label="身份证"
                    placeholder="请输入身份证号码"
                    required
                />
            </van-cell-group>
          </div>
          <div style="position:relative; top:20px ; margin:20px">
            <van-button type="primary" size="large" @click="submit1">确 认</van-button>
          </div>
    </div>
     <div align="center" style="position:relative; top:30px" v-if="!show">
          <div>
              <van-cell-group>
                <van-field
                    id="newPsw"
                    v-model="newPsw"
                    label="新密码"
                    type="password"
                    placeholder="请输入新密码"
                    required
                />

                <van-field
                    id="newPsw2"
                    v-model="newPsw2"
                    label="确认密码"
                    type="password"
                    placeholder="再次请输入新密码"
                    required
                />

            </van-cell-group>
          </div>
          <div style="position:relative; top:20px ; margin:20px">
            <van-button type="primary" size="large" @click="submit2">提 交</van-button>
          </div>
    </div>
    </div>
</template>
<script>
import { Dialog } from 'vant';  //信息提示
import { Toast } from 'vant';
import axios from "axios";
export default {
data() {
    return {
      userName:'',
      stuID:'',
      phone:'',
      IDcard:'',
      newPsw:'',
      newPsw2:'',
      show: true,
    }
  },
    computed:{
        uN(){
            return this.$store.state.uN
        },
    },
   methods: {
    //返回
    onClickLeft() {
      this.$router.go(-1)
    },
    //确定信息1
    submit1(){
      axios({
            method: "post",
            url: "http://localhost:3000/getUserMsg",
            data: {
            uName: this.userName,
            },
            headers: {
            "Content-Type": "text/plain"
            }
        }).then(result => {
            
             if(this.userName==result.data[0].uName 
                && this.stuID==result.data[0].stuID 
                && this.phone==result.data[0].phoneNum
                && this.IDcard==result.data[0].IDcard){
                   Toast.success('信息正确');
                   this.show = false;
                }else{
                  Dialog.alert({
                    message: "信息有误！请重新填写！"
                    }).then(() => {
                    // on close
                    })
                }
        })
    },
    //确认信息2,提交新密码
    submit2(){
      if(this.newPsw==this.newPsw2){
        axios({
            method: "post",
            url: "http://localhost:3000/ResetPsw",
            data: {
            uName: this.userName,
            newPsw: this.newPsw,
            },
            headers: {
            "Content-Type": "text/plain"
            }
        }).then(result => {
             console.log(result);
                Dialog.alert({
                message: "新密码设置完成!"
                }).then(() => {
                    this.newPsw='';
                    this.newPsw2='';
                    this.$router.push('/login');
                })
                         
        })
      }else{
              Dialog.alert({
                message: "密码不相符!"
                }).then(() => {
                    this.newPsw='';
                    this.newPsw2='';
                })
            }   
    }
  }   
}
</script>
<style>

</style>
